<?php
namespace Admin\Controller;
class CarController extends AdminController{
	//车辆
    protected function _initialize() {
		parent::_initialize ();
	}
	//品牌导入
	public function import_brand(){
		 if(IS_POST){	        
            set_time_limit(0);
	        ignore_user_abort(true);//检测用户断开
			if (!empty ( $_FILES ['photo'] ['name'] )){
			    $tmp_file = $_FILES ['photo'] ['tmp_name'];
			    $file_types = explode ( ".", $_FILES ['photo'] ['name'] );
			    $file_type = $file_types [count ( $file_types ) - 1];
			     /*判别是不是.xls文件，判别是不是excel文件*/
			     if ($file_type !== "xls"){
			          $this->error('不是Excel文件，重新上传');
		    }
	     $savePath = './Uploads/excel/';
	     $str = date ( 'Ymdhis' );
	     $file_name = $str . "." . $file_type;
	     if (!move_uploaded_file($_FILES['photo']['tmp_name'],$savePath.$file_name)){
	        $this->error('上传失败！');
	      }
	      $res = $this->read($savePath . $file_name);
	      unset($res[1]);
		}else{
			$this->error('附件不能为空！');
		}
		$tems  = 0;     
		foreach ($res as $k => $v) {
			$data['initial']=$v[1];
			$data['brand_name']=$v[2];
			$brand_info=$this->getInfo('*','brand','brand_id='.$v[3]);
			if($brand_info){
               $this->error('该品牌已存在');
			}
			$data['brand_id']=$v[3];
			$result=M('brand')->add($data);
			if(!$result){
               $this->error('添加失败');
			}
		    $tems++;	
		}
		    $this->success("批量执行成功了：".$tems."条");
	    }else{
	    	$this->display();
	    }
		
	}
	//车系导入
		public function import_series(){
		 if(IS_POST){	        
            set_time_limit(0);
	        ignore_user_abort(true);//检测用户断开
			if (!empty ( $_FILES ['photo'] ['name'] )){
			    $tmp_file = $_FILES ['photo'] ['tmp_name'];
			    $file_types = explode ( ".", $_FILES ['photo'] ['name'] );
			    $file_type = $file_types [count ( $file_types ) - 1];
			     /*判别是不是.xls文件，判别是不是excel文件*/
			     if ($file_type !== "xls"){
			          $this->error('不是Excel文件，重新上传');
		    }
	     $savePath = './Uploads/excel/';
	     $str = date ( 'Ymdhis' );
	     $file_name = $str . "." . $file_type;
	     if (!move_uploaded_file($_FILES['photo']['tmp_name'],$savePath.$file_name)){
	        $this->error('上传失败！');
	      }
	      $res = $this->read($savePath . $file_name);
	      unset($res[1]);
		}else{
			$this->error('附件不能为空！');
		}
		$tems  = 0;     
		foreach ($res as $k => $v) {
			$series_info=$this->getInfo('*','series','car_series_id='.$v[1]);
			if($series_info){
               $this->error('该车系已存在');
			}
			$data['car_series_id']=$v[1];
			$data['initial']=$v[2];
			$data['brand_id']=$v[3];
			$data['car_series_name']=$v[4];
			$data['manufacturer']=$v[5];
			$result=M('series')->add($data);
			if(!$result){
               $this->error('添加失败');
			}
		    $tems++;	
		}
		    $this->success("批量执行成功了：".$tems."条");
	    }else{
	    	$this->display();
	    }
		
	}

		//车辆导入
	public function import_car(){
		 if(IS_POST){	        
            set_time_limit(0);
	        ignore_user_abort(true);//检测用户断开
			if (!empty ( $_FILES ['photo'] ['name'] )){
			    $tmp_file = $_FILES ['photo'] ['tmp_name'];
			    $file_types = explode ( ".", $_FILES ['photo'] ['name'] );
			    $file_type = $file_types [count ( $file_types ) - 1];
			     /*判别是不是.xls文件，判别是不是excel文件*/
			     if ($file_type !== "xls"){
			          $this->error('不是Excel文件，重新上传');
		    }
	     $savePath = './Uploads/excel/';
	     $str = date ( 'Ymdhis' );
	     $file_name = $str . "." . $file_type;
	     if (!move_uploaded_file($_FILES['photo']['tmp_name'],$savePath.$file_name)){
	        $this->error('上传失败！');
	      }
	      $res = $this->read($savePath . $file_name);
	      unset($res[1]);
		}else{
			$this->error('附件不能为空！');
		}
		$tems  = 0;     
		foreach ($res as $k => $v) {
			$data['initial']=$v[1];//首字母
			$data['brand_name']=$v[2];//品牌
			$data['brand_id']=$v[3];//品牌id
			$data['car_series_name']=$v[4];//车系
			$data['car_series_id']=$v[5];//车系id
			$data['type_name']=$v[6];//车型名称
			//判断该车辆是否存在
			$car_info=$this->getInfo('*','car','type_id='.$v[7]);
			if($car_info){
               $this->error('该车辆已存在');
			}
			$data['type_id']=$v[7];//车型id
			$data['guide_price']=$v[8];//厂商指导价
			$data['seat_num']=$v[9];//座位数
			$result=M('car')->add($data);
			if(!$result){
               $this->error('添加失败');
			}
		    $tems++;	
		}
		    $this->success("批量执行成功了：".$tems."条");
	    }else{
	    	$this->display();
	    }
		
	}


	   public function read($filename,$encode='utf-8'){
		      include VENDOR_PATH . 'PHPExcel/PHPExcel.php';
		     // include VENDOR_PATH . 'PHPExcel/PHPExcel/IOFactory.php';
	          $objReader = \PHPExcel_IOFactory::createReader('Excel5');
	          $objReader->setReadDataOnly(true);
	          $objPHPExcel = $objReader->load($filename);
	          $objWorksheet = $objPHPExcel->getActiveSheet();
			  $highestRow = $objWorksheet->getHighestRow();
			  $highestColumn = $objWorksheet->getHighestColumn();
			  $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
			  $temp  = array();
				for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
				    for ($column = 0; $column < $highestColumnIndex; $column++) {//列数是以A列开始
				       $temp[$row][] =$objWorksheet->getCellByColumnAndRow($column,$row)->getValue();
				    }
				}
			 return $temp;
   } 

}